// 棋盘大小
export const boardRowSize = 15;
export const boardColSize = 15;

/**
 * 模拟落子
 * @param gameMapStr
 * @param to
 * @return {any}
 */
export const simulateChessMove = (gameMapStr, to) => {
  // const gameMap = JSON.parse(gameMapStr);
  // // 先移除被吃的棋子
  // const toChessIndex = gameMap.findIndex(chess => chess.x === to.x && chess.y === to.y);
  // if (toChessIndex !== -1) {
  //   gameMap.splice(toChessIndex, 1);
  // }
  // // 模拟走棋
  // const updateChess = gameMap.find(chess => chess.x === from.x && chess.y === from.y);
  // const updateChessIndex = gameMap.findIndex(chess => chess.x === from.x && chess.y === from.y);
  // updateChess.x = to.x;
  // updateChess.y = to.y;
  // gameMap.splice(updateChessIndex, 1, updateChess);
  // return gameMap;

  return [];
};


/**
 * 棋盘列表转成棋盘二维数组
 * @param gameMap
 * @return {[]}
 */
export const listToArray = gameMap => {
  const result = [];
  // 坐标初始化
  for (let i = 0; i < boardRowSize; ++i) {
    result[i] = [];
    for (let j = 0; j < boardColSize; ++j) {
      result[i][j] = null;
    }
  }
  // 遍历所有可用节点
  gameMap.map(chess => result[chess.x][chess.y] = chess);
  return result;
};

/**
 * 棋盘二维数组转成列表
 * @param board
 * @returns {[]}
 */
export const arrayToList = board => {
  const gameMap = [];
  // 坐标初始化
  for (let i = 0; i < board.length; ++i) {
    for (let j = 0; j < board[i].length; ++j) {
      const chess = board[i][j];
      if (chess) {
        gameMap.push(chess);
      }
    }
  }
  return gameMap;
};

/**
 * 秒转成字符串分钟(如：60 -> 01:00)
 * @param seconds
 * @return {string}
 */
export const secondsToMinuteStr = seconds => {
  if (!seconds || seconds <= 0) {
    return '00:00';
  } else {
    const minute = Math.floor(seconds / 60);
    const overSeconds = seconds % 60;
    const minuteStr = minute < 10 ? `0${minute}` : minute;
    const secondsStr = overSeconds < 10 ? `0${overSeconds}` : overSeconds;
    return `${minuteStr}:${secondsStr}`;
  }
};


/**
 * 坐标转换（棋盘镜像）
 * @param to
 * @return {{x: number, y: number}}
 */
export const transformPosition = (to) => {
  const rowSize = boardRowSize - 1;
  const colSize = boardColSize - 1;
  return {...to, x: rowSize - to.x, y: colSize - to.y}
};
